﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Data.SQLite;
using C1.WPF.C1DataGrid;
using C1.WPF.Extended;
using Classes;

namespace GUI
{
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
      #region Member Variables

      SQLiteConnection  database =  new SQLiteConnection(@"Data Source=C:\Users\toadzky\Documents\Visual Studio 2010\Projects\APX_2010\2010.apx");
      Dictionary<String, BindingList<Install>> Searches = new Dictionary<String, BindingList<Install>>();

      #endregion
      
      public MainWindow()
      {
         Staff.Setup();
         OfficeList.Setup();
         EquipmentList.Setup();

         InitializeComponent();

         #region Create Account Lists

         Searches.Add("Search", new BindingList<Install>());
         Searches.Add("Filter", new BindingList<Install>());
         Searches.Add("Employee", new BindingList<Install>());
         Searches.Add("Office", new BindingList<Install>());

         BuildMasterList();

         #endregion

         #region Populate Combo Boxes

         combo_Rep.ItemsSource = Staff.Employees;
         combo_Rep.DisplayMemberPath = "LastFirst";
         
         combo_Office.ItemsSource = OfficeList.List;
         combo_Office.DisplayMemberPath = "Display";

         database.Open();

            DataSet lists = new DataSet();
            lists.Tables.Add(new DataTable("cities"));
            lists.Tables.Add(new DataTable("zips"));

            SQLiteDataAdapter cities = new SQLiteDataAdapter("SELECT DISTINCT City FROM Accounts;", database);
            SQLiteDataAdapter zips = new SQLiteDataAdapter("SELECT DISTINCT Zip FROM Accounts;", database);

            cities.Fill(lists.Tables["cities"]);
            zips.Fill(lists.Tables["zips"]);

            combo_City.DataContext = lists;
            combo_Zip.DataContext = lists;

            combo_State.ItemsSource = StateList.Names;

            combo_Rep.SelectedIndex = combo_Office.SelectedIndex = combo_City.SelectedIndex = combo_State.SelectedIndex = combo_Zip.SelectedIndex = 0;

         database.Close();

         date_FilterInstalled.SelectedDate = DateTime.Today;

         combo_CurrentOffice.ItemsSource = OfficeList.List;
         combo_CurrentOffice.DisplayMemberPath = "Display";
         combo_CurrentRep.ItemsSource = Staff.Employees;
         combo_CurrentRep.DisplayMemberPath = "LastFirst";

         combo_CurrentState.ItemsSource = StateList.Abbreviations;

         combo_CurrentRep.SelectedIndex = combo_CurrentOffice.SelectedIndex = 0;
         #endregion
         
         #region Bind DataGrids

         data_Employees.ItemsSource = Staff.Employees;
         data_Offices.ItemsSource = OfficeList.List;
         
         #endregion
      }

      private void BuildMasterList()
      {
         try
         {
            database.Open();
            SQLiteDataReader filter = new SQLiteCommand("SELECT * FROM Accounts;", database).ExecuteReader();

            while (filter.Read())
               Searches["Filter"].Add(new Install(filter));

            database.Close();
         }
         catch (Exception e)
         {
            Console.Out.WriteLine(e.Message);
         }
      }

      private void data_Employees_SelectedGridItemChanged(object sender, C1.WPF.C1DataGrid.SelectedGridItemChangedEventArgs e)
      {
         try
         {
            database.Open();

            String select = "SELECT * FROM Accounts WHERE Rep='" + ((Employee)e.SelectedItem.Row.DataItem).FirstLast + "';";
            SQLiteCommand cmd = new SQLiteCommand(database);
            cmd.CommandText = select;
            SQLiteDataReader results = cmd.ExecuteReader();

            Searches["Employee"].Clear();

            while (results.Read())
               Searches["Employee"].Add(new Install(results));

            data_AccountList.ItemsSource = Searches["Employee"];

            database.Close();

         }
         catch (Exception)
         {
            
            throw;
         }
      }

      private void data_Offices_SelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
      {
         try
         {
            database.Open();

            String select = "SELECT * FROM Accounts WHERE Office='" + ((Office)e.SelectedItem.Row.DataItem).ID + "';";
            SQLiteCommand cmd = new SQLiteCommand(database);
            cmd.CommandText = select;
            SQLiteDataReader results = cmd.ExecuteReader();

            Searches["Office"].Clear();

            while (results.Read())
               Searches["Office"].Add(new Install(results));

            database.Close();

            data_AccountList.ItemsSource = Searches["Office"];
         }
         catch
         {
         }
      }

      private void SelectedAccordianChanged(object sender, C1.WPF.PropertyChangedEventArgs<bool> e)
      {
         try
         {
            if (accordian_Search.IsExpanded)
               data_AccountList.ItemsSource = Searches["Search"];
            else if (accordian_Filter.IsExpanded)
               data_AccountList.ItemsSource = Searches["Filter"];
            else if (accordian_Employees.IsExpanded)
               data_AccountList.ItemsSource = Searches["Employee"];
            else if (accordian_Offices.IsExpanded)
               data_AccountList.ItemsSource = Searches["Office"];
         }
         catch
         {
         }
      }

      private void SelectedAccountChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
      {
         try
         {
            Binding a = new Binding();
            a.Source = e.AddedItems[0] as Install;
            a.Mode = BindingMode.OneWay;
            grid_InstallDisplay.SetBinding(StackPanel.DataContextProperty, a);

            text_Comments.Text = (e.AddedItems[0] as Install).Comments;
            
            if ( (e.AddedItems[0] as Install).Account.Acct_Type == "Digital" )
               radio_Digital.IsChecked = true;
            else radio_2Way.IsChecked = true;

            //combo_CurrentOffice.SelectedItem = (e.AddedItems[0] as Install).Account.InstallingOffice;
            
            if (grid_InstallDisplay.Visibility == System.Windows.Visibility.Hidden)
               grid_InstallDisplay.Visibility = System.Windows.Visibility.Visible;
         } 
         catch 
         {
         }
      }

      private void menu_AddSensor_Click(object sender, RoutedEventArgs e)
      {
         NewSensor dialog = new NewSensor();

         dialog.ShowDialog();

         if (dialog.DialogResult == true)
         {
            (data_AccountList.CurrentItem as Install).System.Sensors.Add(dialog.toBeAdded);

            database.Open();
               SQLiteCommand update = new SQLiteCommand(
                  String.Format("REPLACE INTO Accounts {0};", (data_AccountList.CurrentItem as Install).DBInsert),
                  database);
               update.ExecuteNonQuery();
            database.Close();

            BuildMasterList();
         }
      }

      private void menu_AddAccount_Click(object sender, RoutedEventArgs e)
      {
      }
   }
}
